﻿package sedie 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.text.TextField;
	import game.exchangeddata.HashTable;
	/**
	 * ...
	 * @author Doan Nguyen Thuy Nha Truc (Bamboo)	
	 */
	public class HistoryPanel extends MovieClip
	{
		public var txtHistoryTf:TextField;
		public var bgHis:MovieClip;
		public var result0:MovieClip;
		public var result1:MovieClip;
		public var result2:MovieClip;
		public var result3:MovieClip;
		public var result4:MovieClip;
		
		private var rowNum:int = 6;
		private var rowInd:int = 0;
		private var colInd:int = 0;
		
		private static const RESULT_0:String = "0";
		private static const RESULT_1:String = "1";
		private static const RESULT_2:String = "2";
		private static const RESULT_3:String = "3";
		private static const RESULT_4:String = "4";
		
		public var hisBoard:Sprite = new Sprite();
		
		public function HistoryPanel() 
		{
			hisBoard.x = bgHis.x;
			hisBoard.y = bgHis.y;
			addChild(hisBoard);
		}
		
	
		
		public function initHis(resultArr:Array):void {
			removeHistory();
			if (resultArr.length == 0)
				return;
				
			
			var pos:uint = 0;
			var flagEven:Boolean;
			var flagTemp:Boolean;
			var flagCheckEvenOdd:Boolean;
			var tempHis:Array = new Array();
			tempHis = resultArr[0].split("-");
			if(tempHis[1]%2 == 0){
				flagEven =true;
			}
			else{
				flagEven = false;
			}
			var iconHis:MovieClip = attachIcon(tempHis[1]);
			iconHis.name = tempHis[0];
			for (var i:uint = 1 ; i < resultArr.length; i++) 
			{
				tempHis = new Array();
				tempHis = resultArr[i].split("-");
				flagCheckEvenOdd = checkEvenOdd(tempHis[1]);
				if (flagEven) {
					if (flagCheckEvenOdd) {
						rowInd++;
						flagTemp = true;
					}
					else {
						colInd++;
						rowInd = 0;
						flagTemp = false;
					}
				}else {
					if (flagCheckEvenOdd) {
						colInd++;
						rowInd = 0;
						flagTemp = true;
					}
					else {
						rowInd++;
						flagTemp = false;
					}
				}
				if(rowInd==rowNum){
					colInd++;
					rowInd=0;
				}
				
				iconHis = attachIcon(tempHis[1]);
				iconHis.name = tempHis[0];
				iconHis.name = tempHis[0];
				flagEven = flagTemp;
			}
					
		}
		
		private function checkEvenOdd(value:uint):Boolean {
			if(value % 2 == 0)
				return true;
			return false;
		}
		
		private function removeHistory():void {
			
			var numberChid:int = hisBoard.numChildren;
			if (numberChid> 0)
			{
				for (var j:int = 0; j < numberChid; j++ ) 
				{
					hisBoard.removeChildAt(0);
					
				}
				rowInd = 0;
				colInd = 0;
			}
		}
		
		private function attachIcon(type:String):MovieClip {
				
				var hisIcon:MovieClip = MovieClip(new HistoryIcon());
				hisIcon.result0.visible = false;
				hisIcon.result1.visible = false;
				hisIcon.result2.visible = false;
				hisIcon.result3.visible = false;
				hisIcon.result4.visible = false;
				
				hisBoard.addChild(hisIcon);
				hisIcon.x = colInd * 22;
				hisIcon.y = rowInd * 22;
				if (type ==  HistoryPanel.RESULT_0) {
					hisIcon.result0.visible = true;
				}
				if (type ==  HistoryPanel.RESULT_1) {
					hisIcon.result1.visible = true;
				}	
				if (type ==  HistoryPanel.RESULT_2) {
					hisIcon.result2.visible = true;
				}	
				if (type ==  HistoryPanel.RESULT_3) {
					hisIcon.result3.visible = true;
				}	
				if (type ==  HistoryPanel.RESULT_4) {
					hisIcon.result4.visible = true;
				}	
				
				return hisIcon;
		}
		
		public function initText(hashTableInfo:HashTable):void {
			
			this.txtHistoryTf.text =  hashTableInfo.lookupString("history");
		}
		
		
	}

}